package com.example.work.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.work.entity.TbCheckArea;
import com.example.work.entity.TbCheckAreaScore;
import com.example.work.entity.vo.AreaScoreVo;
import com.example.work.entity.vo.PutAreaScoreVo;
import com.example.work.mapper.TbCheckAreaScoreMapper;
import com.example.work.service.IAreaService;
import com.example.work.service.ITbCheckAreaScoreService;
import com.example.work.service.ITbCheckItemScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ITbCheckAreaScoreServiceImpl extends ServiceImpl<TbCheckAreaScoreMapper, TbCheckAreaScore> implements ITbCheckAreaScoreService {

    @Autowired
    private TbCheckAreaScoreMapper tbCheckAreaScoreMapper;

    @Autowired
    private IAreaService areaService;

    @Autowired
    private ITbCheckItemScoreService itemScoreService;

    @Override
    public List<AreaScoreVo> getAreaScoreVo(String categoryId, String batchId, String hospId) {

        List<TbCheckArea> areas = areaService.getCategoryAreas(categoryId);
        List<AreaScoreVo> areaScoreVoList = new ArrayList<>();

        for(TbCheckArea area : areas){
            TbCheckAreaScore areaScore = TbCheckAreaScore.getAreaScoreInstance(area);
            areaScore.setBatchId(batchId);
            areaScore.setHospId(hospId);
            AreaScoreVo vo = new AreaScoreVo();
            vo.setAreaScore(areaScore);
            vo.setAreaName(area.getName());
            vo.setItems(itemScoreService.getAreaItemsVo(area.getId(),batchId,hospId));

            areaScoreVoList.add(vo);
        }

        return areaScoreVoList;
    }

    public List<PutAreaScoreVo> getPutAreaScoreVo(String categoryId, String batchId, String hospId) {

        List<TbCheckArea> areas = areaService.getCategoryAreas(categoryId);
        List<PutAreaScoreVo> areaScoreVoList = new ArrayList<>();

        for(TbCheckArea area : areas){
            TbCheckAreaScore areaScore = TbCheckAreaScore.getAreaScoreInstance(area);
            areaScore.setBatchId(batchId);
            areaScore.setHospId(hospId);
            PutAreaScoreVo vo = new PutAreaScoreVo();
            vo.setAreaScore(areaScore);
            vo.setAreaName(area.getName());
            vo.setItems(itemScoreService.getPutAreaItemsVo(area.getId(),batchId,hospId));

            areaScoreVoList.add(vo);
        }

        return areaScoreVoList;
    }

    @Override
    public List<TbCheckAreaScore> getCategoryArea(String categoryId, String batchId, String hospId) {
        return lambdaQuery()
                .eq(TbCheckAreaScore::getCategoryId,categoryId)
                .eq(TbCheckAreaScore::getBatchId, batchId)
                .eq(TbCheckAreaScore::getHospId, hospId)
                .list();
    }


    @Override
    public int getCategoryAreaCount(String categoryId, String batchId, String hospId) {
        return getCategoryArea(categoryId,batchId,hospId).size();
    }
}
